package com.ocn.li.scmdy.modules.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ocn.li.scmdy.common.utils.QueryExt;
import com.ocn.li.scmdy.common.utils.R;
import com.ocn.li.scmdy.modules.app.annotation.Login;
import com.ocn.li.scmdy.modules.app.form.CoinDetailCount;
import com.ocn.li.scmdy.modules.app.form.UpdateCoinForm;
import com.ocn.li.scmdy.modules.app.service.CoinDetailService;
import com.ocn.li.scmdy.modules.app.service.UserService;
import com.ocn.li.scmdy.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 积分操作接口
 *
 * @author 罗春杰-技术部
 */
@RestController
@RequestMapping("/app/coin")
@Slf4j
public class AppCoinDetailController extends AbstractController {

    @Autowired
    private CoinDetailService coinDetailService;

    @Autowired
    private UserService userService;

    @Login
    @PostMapping("/coinDetail")
    public R coinDetail(@RequestBody Map<String, String> map) {
        Page page = null;
        CoinDetailCount coinDetailCount = null;
        try {
            page = coinDetailService.selectPageCoin(new QueryExt(map));
            coinDetailCount = coinDetailService.selectCastCoinCounts(new QueryExt(map));
            coinDetailCount.setAddCoin(coinDetailService.selectGetCoinCounts(new QueryExt(map)));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
        return R.ok().put("page", page).put("coinDetailCount", coinDetailCount);
    }

    @Login
    @PostMapping("/updateCoin")
    public R updateCoin(@RequestBody UpdateCoinForm form) {
        try {
            Integer integer = userService.updateCoin(form);
            if (integer == 1) {
                return R.ok("积分增加成功");
            } else if (integer == 0) {
                return R.ok("积分扣除成功");
            }

        } catch (Exception e) {
            return R.error(e.getMessage());
        }
        return R.error("积分操作失败");
    }

}
